/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer.storage;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.zip.GZIPInputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import org.heresylabs.rostercomposer.ComposerFrame;
import org.heresylabs.rostercomposer.roster.RosterEntry;
import org.heresylabs.rostercomposer.units.Army;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class RosterStorage implements java.io.Serializable {

    private Army army;
    private List<RosterEntry> roster;

    public Army getArmy() {
        return army;
    }

    public void setArmy(Army army) {
        this.army = army;
    }

    public List<RosterEntry> getRoster() {
        return roster;
    }

    public void setRoster(List<RosterEntry> roster) {
        this.roster = roster;
    }

    public static void openRoster() {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Open roster");
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(false);
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.exists() && (f.isDirectory() || f.getName().endsWith(".rstr"));
            }

            @Override
            public String getDescription() {
                return "*.rstr (RosterComposer saved rosters)";
            }

        });
        int res = fc.showSaveDialog(null);
        if (res != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File file = fc.getSelectedFile();
        if (file == null || !file.exists()) {
            return;
        }

        RosterStorage storage = readFromXml(file);

        // it is possible that storage is still empty, cheking:
        if (storage == null) {
            JOptionPane.showMessageDialog(null, "File is not valid RosterComposer format", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else {
            List<RosterEntry> roster = storage.getRoster();
            if (roster != null && roster.size() > 0) {
                for (int i = 0; i < roster.size(); i++) {
                    RosterEntry rosterEntry = roster.get(i);
                    rosterEntry.calculateCost();
                }
            }
            new ComposerFrame(storage.getArmy(), storage.getRoster());
        }
    }

    private static RosterStorage readFromXml(File file) {
        InputStream in = null;
        try {
            in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
            XMLDecoder decoder = new XMLDecoder(in);
            Object o = decoder.readObject();
            decoder.close();
            return (RosterStorage) o;
        }
        catch (Exception ignore) {
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (Exception ignore) {
                }
            }
        }
        return null;
    }

}
